using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
using Microsoft.BizTalk.ExplorerOM;

namespace Conducido.Utils.Bts2006.BTSApplicationUtil.Presentation.ConsoleUI
{
    /// <summary>
    /// Util clas.
    /// </summary>
    public class BtsInstancesHelper
    {
        /// <summary>
        /// Terminates all instances for all orchestration
        /// </summary>
        /// <param name="SelectedBizTalkApplication">The BizTalk application where all the instances needs to be terminated</param>
        public static void TerminateAllInstances(Application SelectedBizTalkApplication)
        {
            if (SelectedBizTalkApplication.Orchestrations.Count > 0)
            {
                foreach (BtsOrchestration orch in SelectedBizTalkApplication.Orchestrations)
                {
                    orch.AutoTerminateInstances = true;
                }
            }
            else
            {
                Console.WriteLine(
                    string.Format("There are no orchestrations in BtsApplication: {0}", SelectedBizTalkApplication.Name));
            }
        }

        /// <summary>
        /// Resumes all suspended instances
        /// </summary>
        /// <param name="selectedBizTalkApplication">The BizTalk application where all the instances needs to be resumed</param>
        public static void ResumeAllInstances(Application selectedBizTalkApplication)
        {
            if (selectedBizTalkApplication.Orchestrations.Count > 0)
            {
                foreach (BtsOrchestration orch in selectedBizTalkApplication.Orchestrations)
                {
                    orch.AutoResumeSuspendedInstances = true;
                }
            }
            else
            {
                Console.WriteLine(
                    string.Format("There are no orchestrations in BtsApplication: {0}", selectedBizTalkApplication.Name));
            }
        }

        /// <summary>
        /// Shows all application of the current BizTalk server
        /// </summary>
        /// <param name="currentHostExplorer"></param>
        /// <returns></returns>
        public static IList<string> ShowAllApplications(BtsCatalogExplorer currentHostExplorer)
        {
            IList<string> applicationList = new List<string>();
            foreach (Application btsApplication in currentHostExplorer.Applications)
            {
                applicationList.Add(btsApplication.Name);
            }
            return applicationList;
        }

        /// <summary>
        /// Shows all application with it's application status
        /// </summary>
        /// <param name="currentHostExplorer"></param>
        /// <returns></returns>
        public static IList<string> ShowAllApplicationsWithStatus(BtsCatalogExplorer currentHostExplorer)
        {
            IList<string> applicationList = new List<string>();
            foreach (Application btsApplication in currentHostExplorer.Applications)
            {
                
                string applicationLine = string.Format("{0} - {1}", btsApplication.Name, btsApplication.Status.ToString());
                applicationList.Add(applicationLine);
            }
            return applicationList;
        }
    }
}
